#include "widget.h"
#include "gamescene.h"
#include "qtimer.h"
#include "ui_widget.h"
#include<gamedefine.h>



Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{



    ui->setupUi(this);
   /* player = new QMediaPlayer(this);
    audioOutput = new QAudioOutput(this);
    player->setAudioOutput(audioOutput );
    audioOutput->setVolume(0.5);  //调节音频音量
    player->setSource(QUrl("qrc:/gameresource/music2.mp3"));
    player->play();*/
    // 初始化主菜单页：连接按钮点击信号
    connect(ui->startbutton, &QPushButton::clicked, this, &Widget::switchToGamePage);
    connect(ui->restartgame,&QPushButton::clicked,this,&Widget::restart);
    connect(ui->exit1,&QPushButton::clicked,this,&Widget::close);
    connect(ui->exitbutton,&QPushButton::clicked,this,&Widget::close);
    connect(ui->winstart,&QPushButton::clicked,this,&Widget::restart);
    connect(ui->winexit,&QPushButton::clicked,this,&Widget::close);
    connect(ui->nextgame,&QPushButton::clicked,this,&Widget::switchtonextGamePage);
    connect(ui->realwinstart,&QPushButton::clicked,this,&Widget::switchToGamePage);
    connect(ui->realwinexit,&QPushButton::clicked,this,&Widget::close);
}
void Widget::switchToGamePage() {
    // 切换到游戏场景页
    ui->stackedWidget->setCurrentWidget(ui->gamepage);
    // 初始化游戏场景（仅一次）
    if (!gameScene) {

        gameScene = new GameScene(this);

        gameScene->setSceneRect(0,0,560,320);
        //setbackground();
        map=new maploader(gameScene);
        map->loadLevel(":/gameresource/whatagameitis.tmj");
        gameScene->gameplayer=newplayer;
        newplayer->setwinapplenumber(map->applepoint->size());
        ui->gameview->centerOn(gameScene->sceneRect().center());

        ui->gameview->setScene(gameScene);
        ui->gameview->setFocusPolicy(Qt::StrongFocus);
        QObject::connect(ui->gameview,&GameView::movePlayerLeft,newplayer,&Player::moveleft);
        QObject::connect(ui->gameview,&GameView::movePlayerRight,newplayer,&Player::moveright);
        QObject::connect(ui->gameview,&GameView::playerJump,newplayer,&Player::jump);
        QObject::connect(ui->gameview,&GameView::stopPlayer,newplayer,&Player::stop);//连接角色移动和键盘输入
        QObject::connect(newplayer,&Player::gameover,ui->gameview,&GameView::gameover);
        QObject::connect(newplayer,&Player::gamewin,this,&Widget::switchgamewinpage);
        QObject::connect(newplayer->m_hitAnimation,&anmation::animationFinished,this,&Widget::switchgameoverpage);


    }
    else{
        newplayer->restart();
        map->applerestart();
        ui->gameview->setEnabled(true);
        ui->gameview->setFocusPolicy(Qt::StrongFocus);
        gameScene->gameTimer->start(16);
    }
}

void Widget::switchgameoverpage()
{
    if(nextnewplayer!=nullptr){
        nextnewplayer->restart();

    }
    ui->stackedWidget->setCurrentWidget(ui->gameover);
    gameScene->gameTimer->stop();
    if(gameScenetwo!=nullptr){
    gameScenetwo->gameTimer->stop();
    }

}

void Widget::restart()
{

    map->applerestart();
    newplayer->restart();
    ui->stackedWidget->setCurrentWidget(ui->gamepage);
    ui->gameview->setEnabled(true);
    ui->gameview->setFocusPolicy(Qt::StrongFocus);
    gameScene->gameTimer->start(16);
}

void Widget::switchgamewinpage()
{
    ui->stackedWidget->setCurrentWidget(ui->gamewin);
    gameScene->gameTimer->stop();
}
Widget::~Widget()
{
    delete ui;
}

void Widget::switchtorealwin()
{
    ui->stackedWidget->setCurrentWidget(ui->realrealwin);
    gameScenetwo->gameTimer->stop();
}

void Widget::switchtonextGamePage()
{


    newplayer->restart();
    ui->stackedWidget->setCurrentWidget(ui->gamepage2);
    if (!gameScenetwo) {

        qDebug()<<"have down";
        gameScenetwo = new GameScene(this);

        gameScenetwo->setSceneRect(0,0,560,320);
        //setbackground();
        maptwo=new nextmaploder(gameScenetwo);
        maptwo->loadLevel(":/gameresource/whatagametwo.tmj");
        gameScenetwo->gameplayer=nextnewplayer;
        nextnewplayer->setwinapplenumber(maptwo->applepoint->size());
        ui->nextgameview->centerOn(gameScenetwo->sceneRect().center());

        ui->nextgameview->setScene(gameScenetwo);
        ui->nextgameview->setFocusPolicy(Qt::StrongFocus);
        QObject::connect(ui->nextgameview,&GameView::movePlayerLeft,nextnewplayer,&Player::moveleft);
        QObject::connect(ui->nextgameview,&GameView::movePlayerRight,nextnewplayer,&Player::moveright);
        QObject::connect(ui->nextgameview,&GameView::playerJump,nextnewplayer,&Player::jump);
        QObject::connect(ui->nextgameview,&GameView::stopPlayer,nextnewplayer,&Player::stop);//连接角色移动和键盘输入
        QObject::connect(nextnewplayer,&Player::gameover,ui->nextgameview,&GameView::gameover);
        QObject::connect(nextnewplayer,&Player::gamewin,this,&Widget::switchtorealwin);
        QObject::connect(nextnewplayer->m_hitAnimation,&anmation::animationFinished,this,&Widget::switchgameoverpage);


    }
    else{
        nextnewplayer->restart();
        maptwo->applerestart();
        ui->nextgameview->setEnabled(true);
        ui->nextgameview->setFocusPolicy(Qt::StrongFocus);
        gameScenetwo->gameTimer->start(16);
    }

}

